$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;
    var focus = true;

    //搜索框 获取焦点
    $("#goods_id").focus();
    $("#goods_id").blur(function () {
        if (focus) {
            $("#goods_id").focus();
        }
    });

    $("div.page").on("click", "span.personal", function() {
        localStorage.removeItem("packMachine");
        focus = false;
        $("#goods_id").blur();
    });

    $(".panel-overlay").on("click", function () {
        focus = true;
        setTimeout(function () {
            $("#goods_id").val("").focus();
        }, 1000);
    });

    //根据扫描内容 得到 产品信息
    $("#goods_id").on("keydown", function (event) {
        if (event.keyCode == "13") {
            if (pass == 0) {
                return
            }
            pass = 0;
            var val = $(this).val();
            if (!val) {
                $alert("请扫描排产单号！");
                setTimeout(function () {
                    pass = 1;
                    $("#goods_id").val("").focus();
                }, 1400);
                return;
            }
            $(this).val("");
            loading_show("获取中...");
            $.ajax({
                headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
                type: "post",
                url: url + '/app/ropetransfer/getRopeWorkplanNumber',
                data: {
                    workplanNumber: val,
                },
                dataType: 'json',
                json: 'callback',
                cache: false,
                success: function (res) {
                    pass = 1;
                    loading_hide();
                    if (res.success) {
                        var data = res.result;
                        $(".content .list-block .goodsList_ul").empty();
                        for (var i = 0; i < data.length; i++) {
                            var data_html = '<li><div class="row no-gutter">' +
                                '<div class="col-90">' +
                                '<p class="p_none"><span class="id">' + data[i]._id + '</span></p>' +
                                '<p><span>排产单号:</span><span class="sp goods_pcdh">' +
                                data[i].workplanNumber + '</span><span>工序:</span><span class="sp goods_gx">' +
                                data[i].process + '</span></p><p><span>产品名称:</span><span class="sp goods_cpmc">' +
                                data[i].inventoryName + '</span></p><p><span>产品规格:</span><span class="sp goods_cpgg">' +
                                data[i].inventorySpecification + '</span></p><p><span>产品编码:</span><span class="sp goods_cpbm">' +
                                data[i].inventoryCode + '</span></p></div>' +
                                '<div class="col-10"><span class="icon iconfont icon-msnui-jian pull-right"></span></div></div></li>';
                            $(".content .list-block .goodsList_ul").append(data_html);
                        }
                        $alert("扫描成功", 800);
                        badge_text();
                    } else {
                        setTimeout(function () {
                            $("#goods_id").val("").focus();
                        }, 1400);
                        $alert(res.message);
                    }
                },
                error: function (error) {
                    pass = 1;
                    loading_hide();
                    console.log(error);
                    setTimeout(function () {
                        $("#goods_id").val("").focus();
                    }, 1400);
                    $alert(error.responseText);
                }
            });
        }
    });

    // li 中的 减号 点击 删除 本条
    $(".content").on("click", ".goodsList_ul li", function () {
        var jianhao = $(this);
        var id = jianhao.find("span.id").text();
        var pcdh = jianhao.find("span.goods_pcdh").text();
        $.confirm('确定选择本工序吗？', function () {
            localStorage.setItem("work", JSON.stringify({id: id, pcdh: pcdh}));
            window.location.href = 'ropetransfer_one.html';
        });
    });

    //点击 某一条 数据  改变 背景颜色
    $(document).on("click", ".content .goodsList_ul li", function () {
        $(this).css("background", "#cccccc").siblings("li").css("background", "#ffffff");
    });

    // goodsList中 badge 的值 为 列表中 li 的个数
    function badge_text() {
        var badge_len = $(".content .goodsList_ul li").length;
        //console.log(badge_len);
        var allWeight = 0;
        var allNetWeight = 0;
        for (var i = 0; i < badge_len; i++) {
            allWeight = accAdd(allWeight, $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpmz").html());
            allNetWeight = accAdd(allNetWeight, $(".content .goodsList_ul li").eq(i).find(".row span.goods_cpjz").html());
        }
        $(".details span.good_length").html(badge_len);
        $(".details span.good_weight").html(allWeight);
        $(".details span.good_netWeight").html(allNetWeight);
    }

    badge_text();

});